//
//  TaskHeader.m
//  RRQS
//
//  Created by kyjun on 16/3/24.
//
//

#import "TaskHeader.h"

@interface TaskHeader ()
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UILabel* labelShipFee;
@property(nonatomic,strong) UILabel* labelCreateTime;
@property(nonatomic,strong) UIImageView* line;
@end

@implementation TaskHeader

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.topView];
    [self addSubview:self.labelShipFee];
    [self addSubview:self.labelCreateTime];
    [self addSubview:self.line];
}

-(void)layoutConstraints{
    self.topView.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelShipFee.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelCreateTime.translatesAutoresizingMaskIntoConstraints = NO;
    self.line.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    [self.topView addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.labelShipFee addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShipFee attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/2]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShipFee attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShipFee attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelShipFee attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    
    [self.labelCreateTime addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateTime attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/2]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateTime attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateTime attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateTime attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1.0f]];
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
}

#pragma mark =====================================================  Public method
-(void)setEntity:(MOrder *)entity{
    if(entity){
        _entity = entity;
        NSString* time =  @"";
        NSString* mark =  @"";
        if(entity.status == expressStatusWating){
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"接单时间";
        }else if(entity.status == expressStatusIng){
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"接单时间";
        }else{
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"下单时间";
        }
        NSString* money = [NSString stringWithFormat:@"￥%@",entity.shipFee];
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n 本单预计收入",money];
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
    }
}
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        NSString* time =  @"";
        NSString* mark =  @"";
        time = [WMHelper timeStampConvertToDateString:waiMaiDodelHList.add_time formate:@"HH:mm"];
        mark =  @"接单时间";
        
        NSString* money = [NSString stringWithFormat:@"￥%@",waiMaiDodelHList.shipping_fee];
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n 本单预计收入",money];
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
    }
}
- (void)setWaiMaiDodelList:(WJWaiMaiDodelList *)waiMaiDodelList
{
    if(waiMaiDodelList){
        _waiMaiDodelList = waiMaiDodelList;
        NSString* time =  @"";
        NSString* mark =  @"";
        time = [WMHelper timeStampConvertToDateString:waiMaiDodelList.add_time formate:@"HH:mm"];
        mark =  @"接单时间";
        
        NSString* money = [NSString stringWithFormat:@"￥%@",waiMaiDodelList.returnpay];
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n 本单预计收入",money];
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
    }
}

#pragma mark =====================================================  property package
-(UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [UIColor colorWithRed:242/255.f green:242/255.f blue:242/255.f alpha:1.0];
    }
    return _topView;
}
-(UILabel *)labelShipFee{
    if(!_labelShipFee){
        _labelShipFee = [[UILabel alloc]init];
        _labelShipFee.numberOfLines = 0;
        _labelShipFee.lineBreakMode = NSLineBreakByCharWrapping;
        _labelShipFee.font = [UIFont systemFontOfSize:14.f];
        _labelShipFee.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelShipFee;
}

-(UILabel *)labelCreateTime{
    if(!_labelCreateTime){
        _labelCreateTime = [[UILabel alloc]init];
        _labelCreateTime.numberOfLines = 0;
        _labelCreateTime.lineBreakMode = NSLineBreakByCharWrapping;
        _labelCreateTime.font = [UIFont systemFontOfSize:14.f];
        _labelCreateTime.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelCreateTime;
}

-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor =  [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _line;
}

@end
